<?php

namespace Home\Controller;
use Think\AjaxPage;
use Think\Page;
use Think\Verify;

class MsgController extends BaseController {
    /**
     *个人站内信列表
     */
    public function msg(){
        $uid = session('admin_id');
        $map['admin_id'] = array('neq',$uid);
        $users = M('admin')->where($map)->field('admin_id,name')->select();
        $this->assign('users',$users);
        $this->display();
    }

    /**
     * ajax站内信
     */
    public function ajaxmsg(){
        // 搜索条件
        $uid = session('admin_id');
        $type = I('type');
        $status = I('post.status');
        $Model = M('message');
        $where=array();
        $where[] = "addressee = ".$uid;
        if(is_numeric($type)){
            if($type==2){
                $where[] = "sender = ".$uid;
                unset($where[0]);
            }else{
                $where[] = "type = ".$type;
            }
        }
        if(is_numeric($status)){
            $where[] = "status = ".$status;
        }
        
        $where = implode(' AND ', $where);
        $count =  $Model->where($where)->count();
        $Page  = new AjaxPage($count,10);
        //  搜索条件下 分页赋值
        foreach($condition as $key=>$val) {
            $Page->parameter[$key]   =   urlencode($val);
        }
       
        $msg = $Model->where($where)->limit($Page->firstRow.','.$Page->listRows)
                ->order('status asc,send_time desc')
                ->select();
       
        $role = M('admin')->getField('admin_id,name');
        if($msg && $role){
            foreach ($msg as $val){
                if($val['type']==1){
                    $val['name'] = '系统提示';
                    $msg1[] = $val;
                }else{
                    $val['name'] =  $role[$val['sender']];
                    $msg2[] = $val;
                }
            }
        }
       //去重
        $res = array();
        $st = array(
            'replyid' => array(),
        );
        foreach($msg2 as $t) {
            $flag = true;
            foreach($st as $k=>$v) {
                if(in_array($t[$k], $v)) $flag = false;
                $st[$k][] = $t[$k];
            }
            if($flag) $res[] = $t;
        }
        if(!empty($msg1)){
            $data = array_merge($msg1,$res);
        }else{
            $data = $res;
        }
        $show = $Page->show();
        $this->assign('data',$data);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }
    
    /**
     * 发送站内信
     * @time  2016/09/01
     */
    public function doSendMessage()
    {
        $message = I('post.text');//内容
        $type = I('post.type', 0);//个体or全体
        $replyid = I('post.replyid', 0);
        $admin_id = session('admin_id');
        $users = I('post.user');//个体id
        $title = I('post.title');
        $messag = array(
            'sender' => $admin_id,
            'addressee' => $users,
            'message' => $message,
            'send_time' => time(),
            'title' => $title,
            'replyid' => $replyid,
            'type'  => $type,
            'status' => 0,
        );
        if (!empty($users)){
            $cid = M('Message')->data($messag)->add();
            if($cid && $replyid==0){
                $map['replyid'] = $cid;
                $up = M('Message')->where('message_id='.$cid)->data($map)->save();
            }
           
            if($cid){
                adminLog('发送站内信',__ACTION__);
                exit(json_encode(array('errNo' => 0, 'msg' => '发送成功')));
            }else{
                exit(json_encode(array('errNo' => 1, 'msg' => '发送失败')));
            }
        }
    }
    
    /**
     * 标记已读或删除
     */
    public function op(){
        $type = I('post.type');
        $selected_id = I('post.selected');
        $selected_id = implode(',',$selected_id);
        $uid = session('admin_id');
        $Model = M('message');
        if(!in_array($type,array('del','hide')) || !$selected_id || !$uid){
            $rs = array('errNo' => 1, 'msg' => '非法操作');
            exit(json_encode($rs));
        }
        $map  = "addressee =".$uid;
        $map .= " AND message_id IN ({$selected_id})";
        if($type == 'del'){
            //删除
            $row = $Model->where($map)->delete();
            adminLog("删除了站内信",__ACTION__);
        }
        if($type == 'hide'){
            $row = $Model->where($map)->save(array('status'=>1));
        }
        if(!$row){
            $rs = array('errNo' => 1, 'msg' => '操作失败');
        }else{
            $rs = array('errNo' => 0, 'msg' => '操作成功');
        }
        exit(json_encode($rs));
    }
    
    /**
     * 单个删除
     */
    public function del(){
        $mid = I('post.del_id');
        if(!$mid){
            $rs = array('errNo' => 1, 'msg' => '参数不能为空');
            exit(json_encode($rs));
        }
        $row = M('message')->where(array('message_id'=>$mid))->delete();
        if(!$row){
            $rs = array('errNo' => 1, 'msg' => '删除失败');
            adminLog("删除站内信",__ACTION__);
        }else{
            $rs = array('errNo' => 0, 'msg' => '删除成功');
        }
        exit(json_encode($rs));
    }
    
    /**
     * 详细详情
     */
    public function detail($id){
        if(!$id){
            $this->error('参数有误');
        }
        $res['id'] = $id;
        $res['replyid'] = I('get.replyid');
        $res['type'] = I('get.type');
        $res['sender'] = I('get.sender');
        $type = $res['type'];
        $res = json_encode($res);
        $this->assign('res',$res);
        $this->assign('type',$type);
        $this->display();
    }
    
    /**
     * ajaxdetailx
     */
    public function ajaxdetail(){
        $Modal = M('message');
        $res = I('post.res');
        $uid = session('admin_id');
        $id = $res['id'];
        $replyid = $res['replyid'];
        $type = $res['type'];
        $sender = $res['sender'];
        if($replyid==0 && $type==1){
            $data = $Modal->where('message_id='.$id)->find();
            $Modal->where(array("message_id"=>$id))->save(array('status'=>1));
        }else{
            $data = $Modal->where('replyid='.$replyid)->order('send_time desc')->limit(10)->select();
            if(empty($data)){
                $this->error('查询失败，无此消息');
            }
            $role = M('admin')->field('admin_id,name')->select();
            foreach ($data as $key=>$val){
                if($val['status']==0 && $val['addressee'] == $uid){
                    $idArr[] = $val['message_id'];
                }
                foreach ($role as $k=>$v){
                    if($val['sender'] == $v['admin_id']){
                        $data[$key]['name'] = $v['name'];
                    }
                    if($v['admin_id'] == $sender && $sender != $uid){
                        $sendername = $v['name'];
                    }
                    if($val['addressee'] == $v['admin_id'] && $sender == $uid){
                        $sendername = $v['name'];
                    }
                }
            }
            if(!empty($idArr)){
                $set = array(
                    'status'=>1
                );
                foreach($idArr as $k=>$v){
                    $Modal->where('message_id='.$v)->save($set);
                }
            }
        }
        foreach ($data as $k=>$v){
            $num[$k] =$v['message_id'];
        }
        array_multisort($num, SORT_ASC, $data);
        $this->assign('type',$type);
        $this->assign('replyid',$replyid);
        $this->assign('sender',$sender);
        $this->assign('sendername',$sendername);
        $this->assign('data',$data);
        $this->display();
    }
    
}